const wx2my = require('../../../wx2my');
const Behavior = require('../../../Behavior');
var base = require('./../../../config.js');
var app = getApp();
Page({
  data: {
    code: !1,
    imgUrl: '',
    trade_no: '',
    order_id: '',
    // 芝麻信用
    canUseCreditAssessment: my.canIUse('zm-credit-assessment'), //判断组件是否可用
    query: {
      serviceId: '2019111100000000000003791600', // serviceId为商户服务ID，芝麻商户平台可查到
      categoryId: 'CREDIT_HOTEL_ONLINE_ALL', // categoryId为服务类目，芝麻商户平台可查到
    },
    // 芝麻信用是否显示
    showCredit: false
  },
  // 组件内部错误，非必需属性，建议写上用来监听组件内部错误
  onError(event) {
    const { detail = {} } = event
    const { error = {} } = detail
    console.log({ error })
    // todo
  },
  add0: function(m) {
    return m < 10 ? '0' + m : m;
  },
  format: function(shijianchuo) {
    //shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + this.add0(m) + '-' + this.add0(d) + ' ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s);
  },

  onLoad: function(e) {
    console.log(e);
    var o = this;
    o.setData({
      // color: wx2my.getStorageSync(["platform"]).data.color,
      imgUrl: wx2my.getStorageSync(["imgUrl1"]).data,
      order_id: e.id
    })





    //  wx2my.newRequest({
    //   url:base.siteroot,
    //   data: {},
    //   success: (res) => {
    //     console.log(res)
    //     console.log('订单详情请求成功')
    //   },
    //   fail: (err) =>{
    //     console.log(err)
    //   }
    // })
    let that = this;

    wx2my.newRequest({
      url: base.siteroot,
      data: {
        c: 'entry',
        a: 'wxapp',
        do: 'orderdetails',
        m: 'zh_jdgjb',
        order_id: e.id
      },
      success: (e) => {
        console.log('订单详情请求成功');
        // 芝麻预授权
        if (e.data.fundauth_type == 2) {
          that.setData({
            showCredit: true
          })
        } else {
          that.setData({
            showCredit: false
          })
        }
        console.log(e);
        var t = e.data;
        t.arrival_time = t.arrival_time.slice(5, 7) + "月" + t.arrival_time.slice(8, 10) + "日", t.departure_time = t.departure_time.slice(5, 7) + "月" + t.departure_time.slice(8, 10) + "日", o.setData({
          order_info: t,
          time: o.format(t.time * 1000),
          trade_no: e.data.trade_no,
        });
        console.log(that.data.trade_no)
      },
      fail: (err) => {
        console.log(err)
      }
    })

    // app.util.request({
    //   url: "entry/wxapp/orderdetails",
    //   data: {
    //     order_id: e.id
    //   },
    //   success: function (e) {
    //     console.log(e);
    //     var t = e.data;
    //     t.arrival_time = t.arrival_time.slice(5, 7) + "月" + t.arrival_time.slice(8, 10) + "日", t.departure_time = t.departure_time.slice(5, 7) + "月" + t.departure_time.slice(8, 10) + "日", o.setData({
    //       order_info: t,
    //       time: o.format(t.time * 1000)
    //     });
    //   }
    // })

    wx2my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    });

  },
  // 立即付款按钮
  confirmorder: function(e) {
    // var t = app.OpenId,
    //     o = this.data.order_info;

    // wx2my.newRequest({
    //     url: base.siteroot,
    //     data: {
    //     c:'entry',
    //     a:'wxapp',
    //     do:'Alipay',
    //     m:'zh_jdgjb',
    //     user_id,
    //     order_id: a
    //     },
    //     success: function (res) {
    //       console.log(res)
    //       console.log('成功进入详情中的Pay...........')
    //       console.log(res.data.data.trade_no)
    let that = this;

    // 普通支付 当payway为花呗和普通支付的时候
    if (that.data.order_info.pay_method == 1) {
      // 普通支付
      my.tradePay({
        tradeNO: that.data.order_info.trade_no, // 调用统一收单交易创建接口（alipay.trade.create），获得返回字段支付宝交易号trade_no
        success: (res) => {
          my.hideLoading();
          console.log('支付成功。。。。。。。。。。。。')
          console.log(res)
          if (res.resultCode == 9000) {
            wx2my.redirectTo({
              url: './../order/book?orderid=' + that.data.order_id
            });
          }
          if (res.resultCode == 6001) {
            my.alert({
              content: '支付失败',
              success: () => {   
                // wx2my.redirectTo({
                //   url: './../order/book?orderid=' + that.data.order_id
                // });

              },

            });
          }
        },
        fail: (err) => {
          console.log("支付失败")
          wx2my.showToast({
            title: "支付失败"
          })
          my.hideLoading();
          // wx2my.redirectTo({
          //   url: "../order/order?activeIndex=1&index=1"
          // });
        }
      });
    } else if (that.data.order_info.pay_method == 2) {
      // 调用预授权
      my.tradePay({
        orderStr: that.data.order_info.orderStr,
        success: (res) => {
          my.hideLoading();
          if (res.resultCode == 9000) {
            wx2my.redirectTo({
              url: './../order/book?orderid=' + that.data.order_id
            });

          } else {
            my.alert({
              content: '支付失败',
              success: () => {
                // wx2my.redirectTo({
                //   url: './../order/book?orderid=' + that.data.order_id
                // });

              },

            });
          }
        },
      })
    }



    // my.tradePay({
    //   tradeNO: this.data.trade_no, // 调用统一收单交易创建接口（alipay.trade.create），获得返回字段支付宝交易号trade_no
    //   success: (res) => {
    //     console.log('支付成功。。。。。。。。。。。。')
    //     console.log(this.data.order_id)
    //     console.log(res)
    //     if (res.resultCode == 9000) {
    //       wx2my.redirectTo({
    //         url: './../order/book?orderid=' + this.data.order_id
    //       });
    //     }
    //     if (res.resultCode == 6001) {
    //       wx2my.showToast({
    //         title: "支付失败"
    //       })
    //       console.log('6001.....')
    //       //   wx2my.redirectTo({
    //       //    url: "../order/order?activeIndex=1"
    //       //   //  &index=1
    //       // }); 
    //     }
    //     //  wx2my.navigateTo({
    //     //   url: './book?orderid=' + this.data.order_id
    //     // });
    //   },
    //   fail: (err) => {
    //     console.log("支付失败")
    //     wx2my.showToast({
    //       title: "支付失败"
    //     })
    //   }
    // });


    // app.util.request({
    //   url: "entry/wxapp/Pay",
    //   cachetime: "0",
    //   data: {
    //     openid: t,
    //     money: o.total_cost,
    //     order_id: o.id
    //   },
    //   success: function (e) {
    //     wx.requestPayment({
    //       timeStamp: e.data.timeStamp,
    //       nonceStr: e.data.nonceStr,
    //       package: e.data.package,
    //       signType: e.data.signType,
    //       paySign: e.data.paySign,
    //       success: function (e) {
    //         console.log("支付成功")
    //         console.log(e)
    //         wx2my.navigateTo({
    //           url: './book?orderid=' + o.id
    //         });
    //       },
    //       fail: function (e) {
    //         console.log("支付失败")
    //         wx2my.showToast({
    //           title: "支付失败"
    //         });
    //       }
    //     });
    //   }
    // });
  },
  order_more: function(e) {
    wx2my.navigateTo({
      url: "../hotel_list/hotel_info?hotel_id=" + this.data.order_info.seller_id
    });
  },
  see_more: function(e) {
    2 == wx2my.getStorageSync("platform").type ? wx2my.navigateTo({
      url: "../hotel_list/hotel_list"
    }) : wx2my.navigateTo({
      url: "../index/index"
    });
  },
  cancel_order: function(e) {
    var t = e.currentTarget.dataset.id,
      o = e.currentTarget.dataset.hb_id;
    1 == e.currentTarget.dataset.classify ? (console.log(o), app.util.request({
      url: "entry/wxapp/CancelOrder",
      data: {
        order_id: t,
        hb_id: o
      },
      success: function(e) {
        console.log(e), wx2my.showToast({
          title: "订单已取消"
        }), setTimeout(function() {
          wx2my.navigateBack({
            delta: 1
          });
        }, 1500);
      }
    })) : wx2my.showModal({
      title: "",
      content: "钟点房不可以取消"
    });
  },
  apply: function(e) {
    var t = e.currentTarget.dataset.id,
      o = e.currentTarget.dataset.type;
    console.log(t), wx2my.showModal({
      title: "温馨提示",
      content: "是否申请退款？",
      success: function(e) {
        e.confirm ? (console.log("用户点击确定"), 1 == o ? app.util.request({
          url: "entry/wxapp/ApplyOrder",
          data: {
            order_id: t
          },
          success: function(e) {
            console.log(e), wx2my.showToast({
              title: "申请成功"
            }), setTimeout(function() {
              wx2my.navigateBack({
                delta: 1
              });
            }, 1500);
          }
        }) : 2 == o ? app.util.request({
          url: "entry/wxapp/YeRefund",
          data: {
            order_id: t
          },
          success: function(e) {
            console.log(e), wx2my.showToast({
              title: "申请成功"
            }), setTimeout(function() {
              wx2my.navigateBack({
                delta: 1
              });
            }, 1500);
          }
        }) : 3 == o && wx2my.showModal({
          title: "",
          content: "到店付不支持退款"
        })) : e.cancel && console.log("用户点击取消");
      }
    });
  },
  code: function(e) {
    var t = this,
      o = t.data.code;
    1 == o ? t.setData({
      code: !1
    }) : t.setData({
      code: !0
    });
  },
  sele_address: function(e) {
    var a = this.data.order_info,
      n = a.coordinates.split(",");
    wx2my.getLocation({
      type: "gcj02",
      success: function(e) {
        var t = Number(n[0]),
          o = Number(n[1]);
        wx2my.openLocation({
          latitude: t,
          longitude: o,
          name: a.seller_name,
          address: a.seller_address
        });
      }
    });
  },
  go_eveluate: function(e) {
    console.log(this.data.order_info.order_no);
    wx2my.navigateTo({
      url: "evaluate?seller_id=" + this.data.order_info.seller_id + "&order_id=" + this.data.order_info.id + '&order_sn=' + this.data.order_info.order_no
    });
  },
  onReady: function() { },
  onShow: function() { console.log(this.data.imgUrl) },
  onHide: function() { },
  onUnload: function() { },
  onPullDownRefresh: function() { },
  onReachBottom: function() { }
});